# -*- coding:utf-8 -*-
import os

import itertools

import sys
import zipfile
from threading import Thread

dictstr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
           'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
           'e', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@', '#', '$',
           '%', '&', '*', '_']

dictint = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
pwddictfile = []


class Myint(object):
    def __init__(self, minlen, maxlen):
        self.minlen = min(minlen, maxlen) if min(minlen, maxlen) > 3 else 3
        self.maxlen = max(minlen, maxlen) if max(minlen, maxlen) < 5 else 5
        global pwddictfile

    def creatPwdDict(self):
        try:
            for i in range(self.minlen, self.maxlen):
                if not os.path.exists("./PwdDict{}.txt".format(i)):
                    with open("./PwdDict{}.txt".format(i), "w+") as pd:
                        listpwd = list(itertools.product(dictint, repeat=i))
                        for pwd in listpwd:
                            y = "".join(pwd)
                            pd.write(y + "\n")
                pwddictfile.append("./PwdDict{}.txt".format(i))
            print "creat success!!!"
        except:
            pass


def extract_zip(path, pwd):
    zip = zipfile.ZipFile(path, "r")
    try:
        zip.extractall(pwd=pwd)
        print "pwd={0}".format(pwd)
    except:
        pass


def unzip():
    my = Myint(3, 6)
    my.creatPwdDict()
    zippath = "./test.zip"
    for path in pwddictfile:
        with open(path, 'r') as pwdfiles:
            for line in pwdfiles:
                try:
                    thread = Thread(target=extract_zip, args=(zippath, line.replace("\n", "")))
                    thread.start()
                except:
                    pass


# unzip()
# my = Myint(3, 6)
# my.creatPwdDict()

